package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bargainexception.PoorException;
import com.qf.bean.Connector;
import com.qf.enums.CustomerEnum;
import com.qf.mapper.ConnectorMapper;
import com.qf.service.ConnectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConnectorServiceImpl implements ConnectorService {

    @Autowired
    private ConnectorMapper connectorMapper;

    @Override
    public PageInfo<Connector> findAll(Integer page, Integer size, String charityname, String name, Integer impgrade) {
        PageHelper.startPage(page,size);
        Map<String,Object> map=new HashMap<>();
        map.put("charityname",charityname);
        map.put("name",name);
        map.put("impgrade",impgrade);
        List<Connector> connectorList=connectorMapper.findAll(map);
        PageInfo<Connector> pageInfo=new PageInfo<>(connectorList);
        return pageInfo;
    }

    @Override
    public Connector findOne(Integer id) {
        return connectorMapper.findOne(id);
    }

    @Override
    @Transactional
    public void addConnector(Connector connector) {
        //设置默认状态为1,
        connector.setStatus(1);
        int count = connectorMapper.insertSelective(connector);
        if(count!=1){
            throw new PoorException(CustomerEnum.CONNECTOR_ADD_ERROR);
        }
    }
}
